<template>
  <div
    class="modal fade"
    tabindex="-1"
    role="dialog"
    aria-hidden="true"
    :id="id"
  >
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">
            <slot name="title"> 新增用户 </slot>
          </h4>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div class="modal-body">
          <form role="form" class="form-horizontal">
            <div class="row mb-2">
              <label class="control-label col-3 text-end"> 用户名* </label>
              <div class="col-8">
                <input
                  type="text"
                  id="frmUser_username"
                  class="form-control"
                  name="user.username"
                  placeholder="用户名"
                  v-model="user.username"
                  :disabled="user.id > 0"
                />
                <span class="help-block">登录系统用的用户名</span>
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-3 text-end"> 真实姓名* </label>
              <div class="col-8">
                <input
                  type="text"
                  id="frmUser_fullname"
                  class="form-control"
                  name="user.empname"
                  placeholder="真实姓名"
                  v-model="user.empname"
                />
                <span class="help-block"></span>
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-3 text-end"> 手机 </label>
              <div class="col-8">
                <input
                  type="text"
                  id="frmUser_mobile"
                  class="form-control"
                  name="user.mobileNo"
                  v-model="user.mobileNo"
                  placeholder="手机号"
                />
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-3 text-end"> 电子邮件 </label>
              <div class="col-8">
                <input
                  type="text"
                  id="frmUser_email"
                  class="form-control"
                  name="user.email"
                  v-model="user.email"
                  placeholder="电子邮件"
                />
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-3 text-end"> eterm用户名 </label>
              <div class="col-8">
                <input
                  type="text"
                  id="frmUser_email"
                  class="form-control"
                  name="user.etermUsername"
                  v-model="user.etermUsername"
                  placeholder="eterm用户名"
                />
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-3 text-end"> 部门 </label>
              <div class="col-8">
                <select
                  class="form-control"
                  v-model="deptIds"
                  size="6"
                  multiple
                >
                  <template v-for="info in departments">
                    <option
                      :value="info.id"
                      v-if="info.parentId > 0"
                      :key="info.id"
                    >
                      {{ info.name }}
                    </option>
                  </template>
                </select>
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-3 text-end"> 职位 </label>
              <div class="col-8">
                <input
                  type="text"
                  class="form-control"
                  v-model="user.position"
                  placeholder="职务"
                />
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-3 text-end"> 状态 </label>
              <div class="col-8">
                <div class="form-check form-check-inline">
                  <input
                    class="form-check-input"
                    type="radio"
                    value="4"
                    v-model.number="user.status"
                  />
                  <label class="form-check-label text-danger">停用</label>
                </div>
                <div class="form-check form-check-inline">
                  <input
                    class="form-check-input"
                    type="radio"
                    value="1"
                    v-model.number="user.status"
                  />
                  <label class="form-check-label">正常</label>
                </div>
              </div>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary me-2"
            data-bs-dismiss="modal"
          >
            关闭
          </button>
          <button type="button" class="btn btn-primary" @click.stop="ok()">
            确定
          </button>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import $ from "jquery";

export default {
  props: {
    nullable: {
      type: Boolean,
      default: true,
    },
    id: {
      type: String,
      default: "userModal",
    },
    departments: {
      type: Array,
    },
  },
  data() {
    return {
      resolve: "",
      reject: "",
      promise: "",

      user: {
        id: 0,
        username: "",
        empname: "",
        mobileNo: "",
        email: "",
        etermUsername: "",
        position: "",
        status: 0,
      },
      deptIds: [],

      myModal: null,
    };
  },
  methods: {
    ok: function () {
      this.hideModal();

      this.resolve({
        threeCode: this.threeCode,
        cityCode: this.cityCode,
        cityName: this.cityName,
        countryCode: this.countryCode,
        airportName: this.nameCn,
        airportEnglishName: this.nameEn,
        deptIds: this.deptIds
      });
    },
    modal: function (v, deptIds) {
      this.user = v;
      this.deptIds = deptIds;

      this.showModal();

      this.promise = new Promise((resolve, reject) => {
        this.resolve = resolve;
        this.reject = reject;
      });
      return this.promise;
    },
    showModal: function () {
      this.getMyModal().show();
    },
    hideModal: function () {
      this.getMyModal().hide();
    },
    getMyModal: function () {
      if (this.myModal === null) {
        let myModalEl = $(this.$el).get(0);
        this.myModal = new bootstrap.Modal(myModalEl, {
          keyboard: true,
        });
      }

      return this.myModal;
    },
  },
};
</script>
